package com.ld.collegetask;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.View;

import com.google.gson.Gson;
import com.ld.collegetask.beans.BaseJsonBean;
import com.ld.collegetask.databinding.ActivityMemberWalletCashApplyBinding;
import com.ld.collegetask.serverInterface.RequestSerives;
import com.ld.collegetask.utils.AppApplication;
import com.ld.collegetask.utils.SharePreference;

import org.json.JSONObject;

import me.leefeng.promptlibrary.PromptDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class MemberWalletCashApplyActivity extends BaseActivity {
    private ActivityMemberWalletCashApplyBinding binding;

    private Integer userId = 0;
    private float money = 0;
    private PromptDialog promptDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_member_wallet_cash_apply);

        SharePreference sp = new SharePreference(getBaseContext());
        userId = sp.getUserId();

        setTitleView(this, binding.ilTitle, true, true, false, false);
        setLoadingView(this, binding.ilLoading);
        setTitle("提现");

        loading_go();
        findView();
        promptDialog = new PromptDialog(this);
        binding.memberWalletCashApplyAmount.setRegion(0.00f, 0.00f);
        binding.memberWalletCashApplyAmount.setTextWatcher();
    }

    @Override
    public void loading() {
        member_cash_query();
    }

    public void findView() {
        binding.memberWalletCashApplyButton.setOnClickListener(this);
        binding.text4.setOnClickListener(this);
    }

    private void member_cash_query() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(AppApplication.server).addConverterFactory(ScalarsConverterFactory.create()).build();
        RequestSerives requestSerives = retrofit.create(RequestSerives.class);
        Call<String> call = requestSerives.member_cash_query(userId);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Gson gson = new Gson();
                BaseJsonBean base = gson.fromJson(response.body().toString(), BaseJsonBean.class);
                if ("1".equals(base.getCode())) {
                    try {
                        JSONObject json = new JSONObject(base.getJson());
                        money = Float.valueOf(json.get("abalance").toString());
                        binding.textUsableMoney.setText(money + "");
                        binding.memberWalletCashApplyAmount.setRegion(money, 0.00f);
                        binding.memberWalletCashApplyAmount.setTextWatcher();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    loading_end();
                } else {
                    loading_error();
                }

            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                loading_error();
            }
        });
    }

    private void member_cash_apply(String pay) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(AppApplication.server).addConverterFactory(ScalarsConverterFactory.create()).build();
        RequestSerives requestSerives = retrofit.create(RequestSerives.class);
        Call<String> call = requestSerives.member_cash_apply(userId, pay,"1");
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Gson gson = new Gson();
                BaseJsonBean base = gson.fromJson(response.body().toString(), BaseJsonBean.class);
                if ("1".equals(base.getCode())) {
                    promptDialog.dismiss();
                    mActivity.finish();
                } else {
                    promptDialog.showWarn(base.getMsg());
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                promptDialog.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.member_wallet_cash_apply_button:
                String member_wallet_cash_apply_amount_val = binding.memberWalletCashApplyAmount.getText().toString();
                promptDialog.showLoading("正在提交");
                member_cash_apply(member_wallet_cash_apply_amount_val);
                break;
            case R.id.text_4:
                binding.memberWalletCashApplyAmount.setText(money + "");
                break;
        }
    }
}
